<template>
  <div>
    <el-dialog
      :title="flag.isAdd?'用户添加':'用户修改'"
      :visible.sync="flag.isShow"
      @closed="closed"
      @opened="opened"
    >
      <el-form :model="form">
        {{form}}
        <el-form-item label="规格名称" label-width="70px">
          <el-input v-model="form.specsname" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="规格属性" label-width="70px" v-for="(item,index) in arr" :key="index">
          <div class="box">
            <el-input autocomplete="off" v-model="item.val"></el-input>
            <el-button type="primary" v-if="index==0" @click="addArr(index)">新增规格属性</el-button>
            <el-button type="danger" v-else @click="delArr(index)">删除</el-button>
          </div>
        </el-form-item>
        <el-form-item label="状态" label-width="70px">
          <el-switch
            v-model="form.status"
            active-color="#13ce66"
            :active-value="1"
            :inactive-value="2"
          ></el-switch>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="flag.isShow = false">取 消</el-button>
        <el-button type="primary" @click="add" v-if="flag.isAdd">添加</el-button>
        <el-button type="primary" @click="change" v-else>修改</el-button>
      </div>
    </el-dialog>
  </div>
</template>
<script>
import { mapGetters, mapActions } from "vuex";
import { specsAdd, specsEdit } from "../../../request/api";
import { successAlert } from "../../../utils/alert";
export default {
  data() {
    return {
      form: {
        specsname: "",
        attrs: "",
        status: 1
      },
      imgUrl: "",
      val: "",
      arr: [{ val: "" }]
    };
  },
  props: ["flag", "long"],
  computed: {
    ...mapGetters({
      specsInfo: "specs/specsInfo"
    })
  },
  components: {},
  methods: {
    ...mapActions({
      reqSpecsList: "specs/reqSpecsList",
      reqSpecsCount: "specs/reqSpecsCount"
    }),
    closed() {
      if (!this.flag.isAdd) {
        this.clear();
      }
    },
    opened() {
      if (!this.flag.isAdd) {
        this.form.specsname = this.specsInfo.specsname;
        this.form.status = this.specsInfo.status;
        this.form.id = this.specsInfo.id;
        this.arr = this.specsInfo.attrs.map(item => {
          return { val: item };
        });
      }
    },
    change() {
      this.form.attrs = this.arr.map(item => item.val).join(",");
      specsEdit(this.form).then(res => {
        if (res.data.code == 200) {
          successAlert(res.data.msg);
          this.flag.isShow = false;
          this.clear();
          this.reqSpecsList();
        }
      });
    },
    add() {
      this.form.attrs = this.arr.map(item => item.val).join(",");
      specsAdd(this.form).then(res => {
        if (res.data.code == 200) {
          successAlert(res.data.msg);
          this.flag.isShow = false;
          this.clear();
          this.reqSpecsList();
          this.reqSpecsCount();
        }
      });
    },
    //修改图片
    changeFile(e) {
      let file = e.raw;
      //根据文件生成一个图片地址
      this.imgUrl = URL.createObjectURL(file);
      //给form.img
      this.form.img = file;
      this.flag.isShow = false;
    },
    clear() {
      this.form = {
        specsname: "",
        attrs: "",
        status: 1
      };
      this.arr = [{ val: "" }];
    },
    //新增规格属性
    addArr(index) {
      this.arr.push({
        val: this.arr[index].val
      });
    },
    //删除规格属性
    delArr(index) {
      this.arr.splice(index, 1);
    }
  },
  mounted() {}
};
</script>
<style lang='less' scoped>
/deep/.avatar-uploader .el-upload {
  border: 1px dashed #d9d9d9;
  border-radius: 6px;
  cursor: pointer;
  position: relative;
  overflow: hidden;
}
.avatar-uploader .el-upload:hover {
  border-color: #409eff;
}
.avatar-uploader-icon {
  font-size: 28px;
  color: #8c939d;
  width: 178px;
  height: 178px;
  line-height: 178px;
  text-align: center;
}
.avatar {
  width: 178px;
  height: 178px;
  display: block;
}
.box {
  display: flex;
  .el-input,
  .el-button {
    width: 1;
  }
}
</style>